package com.client.service.impl;

import com.client.dto.GoodsAndLanguageDto;
import com.client.mapper.FlowerGoodsMapper;
import com.client.pojo.FlowerGoods;
import com.client.pojo.FlowerLanguage;
import com.client.pojo.FlowerOrder;
import com.client.service.FlowerTypesService;
import com.client.service.FlowerGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.client.service.FlowerLanguageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 晚云
 * @since 2023-09-12
 */
@Service
public class FlowerGoodsServiceImpl extends ServiceImpl<FlowerGoodsMapper, FlowerGoods> implements FlowerGoodsService {

    @Autowired
    private FlowerTypesService typesService;

    @Autowired
    private FlowerGoodsMapper flowerGoodsMapper;

    @Autowired
    private FlowerLanguageService flowerLanguageService;

    /**
     * 首页鲜花查询
     * @param typeId
     * @return
     */
    @Override
    @Transactional
    public List<FlowerGoods> goods_list(Integer typeId) {
        List<String> list =  typesService.select_list(typeId);
        List<FlowerGoods> flowerGoods = flowerGoodsMapper.page_List(list);
        return getFlowerGoods(flowerGoods);
    }

    /**
     * 搜索时查询
     *
     * @param search
     * @return
     */
    @Override
    public List<FlowerGoods> search_like(String search) {
        List<FlowerGoods> list = flowerGoodsMapper.search_like(search);
        return getFlowerGoods(list);
    }

    /**
     * 查询单个商品详细页面
     *
     * @param goodId
     * @return
     */
    @Override
    public GoodsAndLanguageDto selectOne(String goodId) {
        FlowerGoods goods = flowerGoodsMapper.queryOne(goodId);
        FlowerLanguage flowerLanguage = flowerLanguageService.queryById(goodId);
        GoodsAndLanguageDto dto = new GoodsAndLanguageDto();
        BeanUtils.copyProperties(goods,dto);
        BeanUtils.copyProperties(flowerLanguage,dto);
        return dto;
    }

    /**
     * 购买之后增加购买数量
     *
     * @param goodsId
     * @param flowerOrder
     * @return
     */
    @Override
    public boolean updateByCount(String goodsId, List<FlowerOrder> flowerOrder) {
        int count = 0;
        for (FlowerOrder order : flowerOrder) {
            count += order.getOrderCount();
        }
         boolean flag = flowerGoodsMapper.updateCount(goodsId,count);
        return flag;
    }

    /**
     * 根据id差商品
     *
     * @param list
     * @return
     */
    @Override
    public List<FlowerGoods> type_list(List<String> list) {
        List<FlowerGoods> flowerGoods = flowerGoodsMapper.page_List(list);
        return getFlowerGoods(flowerGoods);
    }

    /**
     * 根据类型查询
     *
     * @param typeId
     * @return
     */

    /**
     * 图片分割方法
     * @param list
     * @return
     */
    private List<FlowerGoods> getFlowerGoods(List<FlowerGoods> list) {
        List<FlowerGoods> flowerGoods = new ArrayList<>();
        for (FlowerGoods goods : list) {
            String goodsImg = goods.getGoodsImg();
            String[] split = goodsImg.split(",");
            goods.setGoodsImg(split[0]);
            flowerGoods.add(goods);
        }
        return flowerGoods;
    }
}
